<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/15
 * Time: 11:21
 */
@session_start();

class MyAdmin_Authority extends Base
{
    static function isLogin()
    {
        return !empty($_SESSION['administrator']['id']);
    }

    static function doLogin()
    {
        if (empty($_POST['username']))
            Func_Header::back('Username is required.');
        if (empty($_POST['password']))
            Func_Header::back('Password is required.');
        $rs = Data_Mysql_Table_Administrator::select("*", array("username=? OR email= ?" => array($_POST['username'], $_POST['username'])));
        if (empty($rs[0]['id'])) {
            Func_Header::back('Account does not exist!');
        }
        if ($rs[0]['password'] != Data_Mysql_Table_Administrator::makePassword($_POST['password'])) {
            Func_Header::back("The password is incorrect!");
        }
        if (!empty($_POST['remember'])) {
            session_set_cookie_params(86400 * 7);
        }
        $_SESSION['administrator'] = array(
            'id' => $rs[0]['id'],
            'fullname' => $rs[0]['fullname'],
            'email' => $rs[0]['email'],
            'username' => $rs[0]['username'],
            'register_time' => $rs[0]['register_time'],
            'login_time' => time(),
//            'role_cache' => self::_getRoleCache($rs[0]),
        );
//        q(Data_Mysql_Table_Administrator::select());
        Data_Mysql_Table_Administrator::update($rs[0]['id'], array("login_times = login_times + 1"));
        Func_Header::to();
    }

    static function doRegister()
    {
        if (empty($_POST['fullname']))
            Func_Header::back('Fullname is required.');

        $post = Func_Input::filter(array(
            'email' => 'email'
        ), 'p');

        if (empty($post['email']))
            Func_Header::back('E-mail is required.');

        if (empty($_POST['username']))
            Func_Header::back('Username is required.');

        if (empty($_POST['password']) || empty($_POST['rpassword']) || $_POST['password'] != $_POST['rpassword'])
            Func_Header::back('PassWord is required.');

        $rs = Data_Mysql_Table_Administrator::insert(array(
            'fullname' => $_POST['fullname'],
            'email' => $post['email'],
            'username' => $_POST['username'],
            'password' => Data_Mysql_Table_Administrator::makePassword($_POST['password']),
            'register_time' => time(),
        ));
        if (empty($rs[0])) {
            Func_Header::back('The account already exists!');
        } else {
            $_SESSION['administrator'] = array(
                'id' => $rs[0],
                'fullname' => $_POST['fullname'],
                'email' => $post['email'],
                'username' => $_POST['username'],
                'register_time' => time(),
                'login_time' => time(),
                'role_cache' => array()
            );
            Func_Header::to();
        }
    }

    static function doLogout()
    {
        unset($_SESSION['administrator']);
        Func_Header::to('', Core::$urls['root'] . Core::$urls['package']);
    }
}